package com.ljx.rabbit.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.rabbit.context.BaseContext;
import com.ljx.rabbit.domain.dto.AddCartDTO;
import com.ljx.rabbit.domain.dto.DelIds;
import com.ljx.rabbit.domain.po.Cart;
import com.ljx.rabbit.domain.vo.CartVo;
import com.ljx.rabbit.result.R;
import com.ljx.rabbit.service.ICartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-21
 */
@RestController
@RequestMapping("/member/cart")
@CrossOrigin
@Slf4j
@Api(tags = "购物车")
@RequiredArgsConstructor
public class CartController {
    private final ICartService cartService;

    @GetMapping
    @ApiOperation(value = "获取购物车列表")
    public R<List<CartVo>> getCartList() {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Cart::getUserId, BaseContext.getCurrentId()).eq(Cart::isShown,true );
        List<Cart> cartList = cartService.list(queryWrapper);
        List<CartVo> list=new ArrayList<>();
        for (Cart cart : cartList) {
            CartVo cart1 = new CartVo();
            BeanUtils.copyProperties(cart, cart1);
            list.add(cart1);
        }
        return R.success(list);
    }

    @PostMapping
    @ApiOperation(value = "添加购物车")
    public R<String> addCart(@RequestBody AddCartDTO addCartDTO) {
        cartService.addCart(addCartDTO);
        return R.success("添加成功");
    }

    @DeleteMapping
    @ApiOperation(value = "删除购物车")//1
    public R<String> deleteCart(@RequestBody DelIds delIds) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Cart::getUserId, BaseContext.getCurrentId()).in(Cart::getSkuId, delIds.getIds());
        cartService.remove(queryWrapper);
        return R.success("删除成功");
    }

    @PostMapping("/merge")
    @ApiOperation(value = "合并购物车")
    public R<String> mergeCart(@RequestBody List<AddCartDTO> list) {
        log.info("合并购物车{}",list);
        cartService.mergeCart(list);
        return R.success("合并成功");
    }

    @PutMapping("/status")
    @ApiOperation(value = "修改购物车单间商品状态")
    public R<String> changeStatus(@RequestParam Long skuId,@RequestParam Boolean selected) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Cart::getUserId, BaseContext.getCurrentId()).eq(Cart::getSkuId, skuId).eq(Cart::isShown,true);
        Cart cart = cartService.getOne(queryWrapper);
        cart.setSelected(selected);
        cartService.updateById(cart);
        return R.success("修改成功");
    }

    @PutMapping("/status/all")
    @ApiOperation(value = "修改购物车所有商品状态")
    public R<String> changeStatusAll(@RequestParam Boolean selected) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Cart::getUserId, BaseContext.getCurrentId()).eq(Cart::isShown,true);
        List<Cart> carts = cartService.list(queryWrapper);
        for (Cart cart : carts) {
            cart.setSelected(selected);
        }
        cartService.updateBatchById(carts);
        return R.success("修改成功");
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改购物车商品数量")
    public R<String> updateCart(@RequestParam Long skuId,@RequestParam Integer count) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Cart::getUserId, BaseContext.getCurrentId()).eq(Cart::getSkuId, skuId);
        Cart cart = cartService.getOne(queryWrapper);
        cart.setCount(count);
        cartService.updateById(cart);
        return R.success("修改成功");
    }
}
